#! /usr/bin/python
# Copyright (C) 2010  Johan Jordaan (www.johanjordaan.co.za)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Todo List
#
import os
import os.path
import zipfile
import datetime
from optparse import OptionParser   

def zip_directory(source_dir,archive_name):
  z = zipfile.ZipFile(archive_name,'w',zipfile.ZIP_DEFLATED)
 
  for file_name in os.listdir(source_dir):
    if not os.path.isdir(os.path.join(source_dir,file_name)):
      z.write(os.path.join(source_dir,file_name),file_name)
  z.close()

def release(dest_dir,gatherer_csv_dir,enriched_csv_dir,db_dir):
  if not os.path.exists(dest_dir):
    os.mkdir(dest_dir)

  date_stamp = datetime.datetime.today().strftime('_%Y_%m_%d')
  zip_directory(gatherer_csv_dir,os.path.join(dest_dir,'gatherer_csv'+date_stamp+'.zip'))
  zip_directory(enriched_csv_dir,os.path.join(dest_dir,'enriched_gatherer_csv'+date_stamp+'.zip'))
  zip_directory(db_dir,os.path.join(dest_dir,'mtgt_master'+date_stamp+'.zip'))
  

if __name__ == '__main__':
  parser = OptionParser()
  parser.add_option('-F', action='store_true', dest='force_data',default=False,help='Overwrite all data files')
  parser.add_option('-o', '--output',dest='dest_dir',default=r'..\data\release',help='Directory where the released zip files should be created in')
  parser.add_option('-g', '--gatherer',dest='gatherer_dir',default='..\data\gatherer\csv',help='Directory where the gatherer csv files resides')
  parser.add_option('-e', '--enriched',dest='enriched_dir',default='..\data\enriched\csv',help='Directory where the enriched csv files resides')
  parser.add_option('-d', '--database',dest='db_dir',default='..\data\sqlite',help='Directory where the sqlite database resides')
    
  (options, args) = parser.parse_args()
  
  release(options.dest_dir, options.gatherer_dir, options.enriched_dir, options.db_dir)
